package com.swang.pms.web.controller.common;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.swang.pms.common.util.EnumToDataUtil;
import com.swang.pms.common.util.LoggerUtil;
import com.swang.pms.model.annotation.PltLog;
import com.swang.pms.model.common.Pager;
import com.swang.pms.model.dto.common.SysDictionaryDto;
import com.swang.pms.model.entity.CurrentKey;
import com.swang.pms.model.enums.DictionaryGroup;
import com.swang.pms.model.enums.Status;
import com.swang.pms.model.vo.ResultMessage;
import com.swang.pms.service.common.SysDictionaryService;
import com.swang.pms.web.controller.BaseController;

/**
 * @ClassName: SysDictController
 * @Description: 数据字典服务控制层
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@Controller
@RequestMapping("/dict")
public class SysDictController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(SysDictController.class);

    @Resource
    SysDictionaryService sysDictionaryService;

    @RequestMapping(value = "/page")
    public ModelAndView page(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("param/dictMgr", model);
    }

    @RequestMapping(value = "/editPage")
    public ModelAndView editPage(ModelMap model, String type) {
        model.put("type", type);
        return new ModelAndView("param/win/editDict", model);
    }

    @RequestMapping(value = "/editGroupPage")
    public ModelAndView editGroupPage(ModelMap model, String type) {
        model.put("type", type);
        return new ModelAndView("param/win/editDictGroup", model);
    }

    @PltLog(log = "字典管理分组查询")
    @RequestMapping(value = "/findAllSysDictGroupGrid")
    public @ResponseBody Pager findAllSysDictGroupGrid(Pager pager, SysDictionaryDto sysDictionaryDto) {
        if (sysDictionaryDto != null && StringUtils.isNotBlank(sysDictionaryDto.getDictName())) {
            sysDictionaryDto.setDictName(sysDictionaryDto.getDictName().replaceAll("%", "\\\\%"));
        }
        return this.sysDictionaryService.findAllSysDictGroupGrid(pager, sysDictionaryDto);
    }

    @PltLog(log = "字典管理分页查询")
    @RequestMapping(value = "/findSysDictGrid")
    public @ResponseBody Pager findSysDictGrid(Pager pager, SysDictionaryDto sysDictionaryDto) {
        if (sysDictionaryDto != null && StringUtils.isNotBlank(sysDictionaryDto.getDictName())) {
            sysDictionaryDto.setDictName(sysDictionaryDto.getDictName().replaceAll("%", "\\\\%"));
        }
        return this.sysDictionaryService.pageFindSysDictionaryGrid(pager, sysDictionaryDto);
    }

    @PltLog(log = "增加字典操作")
    @RequestMapping(value = "/addSysDict")
    public @ResponseBody ResultMessage addSysDict(@RequestBody SysDictionaryDto sysDictionaryDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "新增成功");
        CurrentKey currentKey = new CurrentKey();
        currentKey.setId(sysDictionaryDto.getId());
        try {
            SysDictionaryDto sysDictionary = this.sysDictionaryService.getDictionary(sysDictionaryDto);
            if (sysDictionary != null && !Status.DELETE.getCode().equals(sysDictionary.getStatus())) {
                result.change(false, "已存在,请修改枚举编码");
                return result;
            }
            sysDictionaryDto.setGmtCreate(new Date());
            sysDictionaryDto.setGmtModify(new Date());
            sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
            if (sysDictionary != null && Status.DELETE.getCode().equals(sysDictionary.getStatus())) {
                sysDictionaryDto.setId(sysDictionary.getId());
                this.sysDictionaryService.updateByPrimaryKey(sysDictionaryDto);
            } else {
                this.sysDictionaryService.insertSysDict(sysDictionaryDto);
            }
        } catch (Exception e) {
            result.change(false, "新增失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-addSysDict error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "增加字典分组")
    @RequestMapping(value = "/addSysDictGroup")
    public @ResponseBody ResultMessage addSysDictGroup(@RequestBody SysDictionaryDto sysDictionaryDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "新增成功");
        CurrentKey currentKey = new CurrentKey();
        currentKey.setId(sysDictionaryDto.getId());
        sysDictionaryDto.setId(null);

        try {
            sysDictionaryDto.setGroupCode(DictionaryGroup.DICTIONARY_GROUP.getCode());
            sysDictionaryDto.setGroupName(DictionaryGroup.DICTIONARY_GROUP.getName());
            SysDictionaryDto sysDictionary = this.sysDictionaryService.getDictionary(sysDictionaryDto);
            if (sysDictionary != null && !Status.DELETE.getCode().equals(sysDictionary.getStatus())) {
                result.change(false, "已存在,请修改字典编码！");
                return result;
            }
            sysDictionaryDto.setGmtCreate(new Date());
            sysDictionaryDto.setGmtModify(new Date());
            sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
            if (sysDictionary != null && Status.DELETE.getCode().equals(sysDictionary.getStatus())) {
                sysDictionaryDto.setId(sysDictionary.getId());
                this.sysDictionaryService.updateByPrimaryKey(sysDictionaryDto);
            } else {
                this.sysDictionaryService.insertSysDict(sysDictionaryDto);
            }
        } catch (Exception e) {
            result.change(false, "新增失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-addSysDictGroup error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "编辑字典操作")
    @RequestMapping(value = "/editSysDict")
    public @ResponseBody ResultMessage editSysDict(@RequestBody SysDictionaryDto sysDictionaryDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "更新成功");
        CurrentKey currentKey = new CurrentKey();
        currentKey.setId(sysDictionaryDto.getId());
        try {
            int count = this.sysDictionaryService.countOneSysDict(sysDictionaryDto);
            if (count > 0) {
                result.change(false, "已存在,请修改枚举编码");
                return result;
            }
            sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
            sysDictionaryDto.setGmtModify(new Date());
            this.sysDictionaryService.updateByPrimaryKey(sysDictionaryDto);
        } catch (Exception e) {
            result.change(false, "更新失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-editSysDict error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "编辑字典分组操作")
    @RequestMapping(value = "/editSysDictGroup")
    public @ResponseBody ResultMessage editSysDictGroup(@RequestBody SysDictionaryDto sysDictionaryDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "更新成功");
        CurrentKey currentKey = new CurrentKey();
        currentKey.setId(sysDictionaryDto.getId());
        try {
            int count = this.sysDictionaryService.countOneSysDict(sysDictionaryDto);
            if (count > 0) {
                result.change(false, "已存在,请修改字典编码");
                return result;
            }
            sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
            sysDictionaryDto.setGmtModify(new Date());
            this.sysDictionaryService.updateDictGroup(sysDictionaryDto);
        } catch (Exception e) {
            result.change(false, "更新失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-editSysDictGroup error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "删除字典操作")
    @RequestMapping(value = "/delSysDict")
    public @ResponseBody ResultMessage delSysDict(@RequestBody SysDictionaryDto sysDictionaryDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "删除成功");
        CurrentKey currentKey = new CurrentKey();
        currentKey.setId(sysDictionaryDto.getId());
        try {
            this.sysDictionaryService.deleteByPrimaryKey(sysDictionaryDto);
        } catch (Exception e) {
            result.change(false, "删除失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-delSysDict error: {0}", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/getProvince")
    public @ResponseBody ResultMessage getProvince(@RequestBody SysDictionaryDto sysDictionaryDto) {
        ResultMessage result = new ResultMessage(true, "获取省列表成功");
        try {
            sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
            sysDictionaryDto.setGroupCode(DictionaryGroup.AREA_PROVINCE.getCode());
            result.setContent(getComboData(this.sysDictionaryService.getProvince(sysDictionaryDto), ""));
        } catch (Exception e) {
            result.change(false, "获取省列表失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-getProvince error: {0}", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/getCity")
    public @ResponseBody ResultMessage getCity(@RequestBody SysDictionaryDto sysDictionaryDto) {
        ResultMessage result = new ResultMessage(true, "获取城市列表成功");
        try {
            sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
            sysDictionaryDto.setGroupCode(DictionaryGroup.AREA_CODE.getCode());
            result.setContent(getComboData(this.sysDictionaryService.getCity(sysDictionaryDto), ""));
        } catch (Exception e) {
            result.change(false, "获取城市列表失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-getCity error: {0}", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/getProvinceByCity")
    public @ResponseBody ResultMessage getProvinceByCity(@RequestBody SysDictionaryDto sysDictionaryDto) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            SysDictionaryDto dto = sysDictionaryService.getProvinceByCity(sysDictionaryDto);
            SysDictionaryDto dto1 = new SysDictionaryDto();
            dto1.setDictCode(dto.getDictCode());
            dto1.setStatus(Status.PUBLISH.getCode());
            dto1.setGroupCode(DictionaryGroup.AREA_CODE.getCode());
            dto.setCityComboData(getComboData(this.sysDictionaryService.getCity(dto1), ""));
            result.setContent(dto);
        } catch (Exception e) {
            result.change(false, "获取城市列表失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-getCity error: {0}", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/getArea")
    public @ResponseBody ResultMessage getArea(@RequestBody SysDictionaryDto sysDictionaryDto) {
        ResultMessage result = new ResultMessage(true, "获取区县列表成功");
        try {
            sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
            sysDictionaryDto.setGroupCode(DictionaryGroup.AREA_CODE.getCode());
            result.setContent(getComboData(this.sysDictionaryService.getArea(sysDictionaryDto), ""));
        } catch (Exception e) {
            result.change(false, "获取区县列表失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-getArea error: {0}", e.getMessage());
        }
        return result;
    }

}
